﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gman.WebApp.ServiceControllers;
using System.IO;
using System.Text;
using Gman.WebApp.Models;

namespace Gman.WebApp.Controllers
{    
    public abstract partial class BaseController : Controller
    { 
        internal Action<PropertySetter<IValidatableModel>.SetterOptions> _action;
        internal PropertySetter<IValidatableModel>.SetterOptions _options;       
        public BaseController()
        {
            _action = a => PropertySetter<IValidatableModel>.SetFinalObject(a);
            _options = new PropertySetter<IValidatableModel>.SetterOptions();           
        }

        public BaseController(Action<PropertySetter<IValidatableModel>.SetterOptions> action, PropertySetter<IValidatableModel>.SetterOptions options)
        {
            _action = action;
            _options = options;            
        }

        public virtual JsonResult UpdateFieldDelegate(Action<PropertySetter<IValidatableModel>.SetterOptions> action, PropertySetter<IValidatableModel>.SetterOptions options, IValidatableModel model)
        {
            options.objectToSet = model;
            options.validationList = model.Validator.ValidationList;
            model.Validator.ValidationList.Clear();

            action(options);

            return Validate(model);
        }

        private JsonResult Validate(IValidatableModel model)
        {
            model.Validator.ValidationList.Clear();
            model.Validator.HandlePropertyValidation();
            return Json(new
            {
                PropertyValidationList = model.Validator.ValidationList,
            });
        }

        private const string UpdateFieldErrorMessage = "There was a problem updating the field, please try again or contact your administrator.";
        private const string UpdateFieldReportException = "There was a problem updating the field, name : {0} to value : {1} for model id : {2}";
        public JsonResult UpdateFieldBase(string fieldName, string fieldValue, Guid id)
        {
            try
            {
                var model = GetModel(id);
                _options.property = fieldName;
                _options.val = fieldValue;

                return UpdateFieldDelegate(
                    _action,
                    _options,
                    model);

            }
            catch (Exception e)
            {
                return Json(UpdateFieldErrorMessage);
            }
        }

        private const string ValidateErrorMessage = "There was a problem validating the fields, please try again or contact your administrator.";
        private const string ValidateFieldErrorMessage = "There was a problem validating the model for model id : {0}";     
        public virtual JsonResult ValidateBase(Guid id)
        {
            try
            {
                IValidatableModel model = GetModel(id);
                return Validate(model);
            }
            catch (Exception e)
            {                
                return Json(ValidateErrorMessage);
            }
        }

        public virtual IValidatableModel GetModel(Guid id)
        {
            throw new NotImplementedException();
        }
    }
}
